package com.atguigu.gmall.mq.receiver;

import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Component
@Configuration
public class ConfirmReceiver {

    @SneakyThrows
    //监听消息  必须先设置好绑定关系
    //监听器注解
    @RabbitListener(bindings=@QueueBinding(
            value = @Queue(value = "queue.confirm",durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = "exchange.confirm"),
            key = {"routing.confirm"}))
    public void getMsg(String msg, Message message, Channel channel){
        System.out.println("接收的消息：\t" + msg);

        // 采用手动应答模式, 手动确认应答更为安全稳定
        //如果手动确定了，再出异常，mq不会通知；如果没有手动确认，抛异常mq会一直通知
        //channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            // false 确认一个消息，true 批量确认
         channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }
}
